package cz.cuni.amis.episodic.lisp;

import java.util.ArrayList;

/**
 *
 * @author ik
 */
public class DefaultLispTranslator extends LispPlanToCsvData.TreeToTraceTranslator {

    public DefaultLispTranslator(int traceDepthLimit) {
        super(traceDepthLimit);
    }

    
    
    @Override
    public boolean atomicAction(String action, ArrayList<String> trace, ArrayList<String> affordances) {
        int  processedDepth;
        if(traceDepthLimit <= 0) {
            processedDepth = trace.size();
        } else {
            processedDepth = Math.min(trace.size(), traceDepthLimit);
        }
            // filter AND nodes

            String[] filteredTrace = new String[processedDepth];
            int offset = trace.size() - (processedDepth - 1);
            for (int i = 0; i < processedDepth - 1; i++) {
                filteredTrace[i] = translateOutcome(trace.get(offset + i));
            }
            // store
            filteredTrace[filteredTrace.length - 1] = translateOutcome(action);
            traces.add(filteredTrace);
            return true;
    }
    
        /**
     * Translates outcome identifier so it conforms with SMILEs rules.
     *
     * @param outcome
     * @return
     */
    public static String translateOutcome(String outcome) {
        outcome = outcome.replace('-', '_');
        outcome = outcome.replace(' ', '_');
        if (outcome.charAt(0) == '!') {
            return outcome.substring(1) + "_";
        } else {
            return outcome;
        }
    }
    
}
